<?php

use Yaf\Registry;
use models\Business\Oauth;
use models\Business\TencentOauth;
use Task\WsClient;


class QqController extends ApiBaseController
{
    /**
     * qq登陆 跳转到获取扫描二维码登陆页面，用户扫描成功后会跳转到xxx.com/callback/qq/oauth页面
     */
    public function loginAction() {
        // 获取第三方平台的授权登录地址并跳转
        $url = TencentOauth::qqLogin();
        header('location:' . $url);
    }

    public function oauthAction() {
        $parameters = Registry::get('parameters');
        Registry::get('notify_log')->info('qq第三方登陆callback参数: ' . json_encode($parameters));

        // 拉取第三方平台的用户信息
        $parameters = TencentOauth::qqGetUserInfo($parameters['state']);

        // 注册
        $member_info = Oauth::login($parameters);
        $member_info['type'] = $parameters['type'];

        header("Location: " . Registry::get('config')['application']['member']['host'] . '?jwt=' . $member_info['jwt'] . '&login_type=' . $member_info['type']);

        /*try {
            $client = new WsClient('127.0.0.1', 19992, '/');
            if($client->connect())
                $client->sendJson(['action' => 'speakTo', 'state' => $state, 'msg' => 'login successfully',
                    'user_info' => $member_info]);
            else
                Registry::get('user_log')->err(__METHOD__ . " - websocket链接失败");
        } catch (Exception $e) {
            Registry::get('user_log')->err(__METHOD__ . " - 链接失败: " . $e->getMessage());
        } finally {
            return $this->_responseJson($member_info);
        }*/
    }
}
